﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Reflection;

namespace SVAP
{
    public partial class pictureMDI : Form
    {
        internal string filePath = "";    //记录图片的地址
        internal bool isConfigure;        //标志位，表示是否已经配置过

        internal bool flag1 = false;
        internal bool flag2 = false;
        internal bool flag3 = false;

        internal List<string> strList = new List<string>();// 存储用户选择的结果配置，在ConfigAlgorithmForm中更新
        internal List<int> intList = new List<int>();//存储用结果配置的index，方便固定配置的使用

        //还要添加一些可自由使用的

        internal ConfigAlgorithmForm caf = null;

        public pictureMDI()
        {
            InitializeComponent();

            //置标志位
            isConfigure = false;
        }

        //查看图片
        private void btn_viewPic_Click(object sender, EventArgs e)
        {
            if (this.filePath != "")
            {
                try
                {
                    System.Diagnostics.Process.Start(this.filePath);
                }
                catch
                {
                    MessageBox.Show("无法打开图片！");
                }
            }
            else
                MessageBox.Show("请选择图片！");
        }

        //配置
        private void btn_config_Click(object sender, EventArgs e)
        {
            if (caf == null || caf.IsDisposed)
            {
                caf = new ConfigAlgorithmForm();
            }

            caf.Owner = this;
            caf.Show();
            caf.Focus();
        }

        //载入非托管dll
        [DllImport("kernel32.dll")]
        public static extern int ArchCluster(string pstrImage, out string a, out string str1, out string str2);

        //开始分析
        private void btn_analys_Click(object sender, EventArgs e)
        {
            string strNum = "";//保持识别出来的数量
            string strColor = "";//保存识别出来的颜色
            string strShape = "";//保存用户自定义的配置结果
            string strRecContent11 = "识别内容：";
            string strRecContent21 = "识别结果：";
            //string strRecContent31 = "用时：";

            string strRecContent12 = "识别内容：";
            string strRecContent22 = "识别结果：";
            //string strRecContent32 = "用时：";

            string strRecContent13 = "识别内容：";
            string strRecContent23 = "识别结果：";
            //string strRecContent33 = "用时：";

            string pstrImage;// 被分析图片的URL
            //pstrImage = @"2round.bmp";
            pstrImage = MainForm.strPictureURL;
            DateTime dt1 = new DateTime();
            DateTime dt2 = new DateTime();

            if (isConfigure == true)
            {
                ///*
                dt1 = DateTime.Now;
                try
                {
                    ArchCluster(pstrImage, out strNum, out strColor, out strShape);
                }
                catch (System.EntryPointNotFoundException ep)//当dll为托管代码时
                {
                    Object[] t = new Object[4];
                    t[0] = pstrImage;
                    t[1] = "";
                    t[2] = "";
                    t[3] = "";
                    AppDomain ad = AppDomain.CreateDomain("New Domain");
                    ProxyObject obj = (ProxyObject)ad.CreateInstanceFromAndUnwrap(@"SVAP.exe", "SVAP.ProxyObject");
                    try
                    {
                        obj.LoadAssembly();
                        obj.Invoke("RoundRec.Program", "ArchCluster", ref t);
                        AppDomain.Unload(ad);
                        obj = null;
                        strNum = t[1].ToString();
                        strColor = t[2].ToString();
                        strShape = t[3].ToString();
                    }
                    catch (System.BadImageFormatException ep1)//当获取不到dll命名空间时...
                    {
                        MessageBox.Show("算法文件有误");
                    }
                    dt2 = DateTime.Now;
                }
                //*/
                //显示结果
                string strResult = "";

                //if (strNum != ""&&flag1==true)
                if (flag1 == true)
                {
                    strRecContent11 += "车辆数目";
                    strRecContent21 += strNum;
                    strResult += strRecContent11;
                    strResult += "\n";
                    strResult += strRecContent21;
                    strResult += "\n\n";
                }
                //if (strColor != ""&&flag2==true)
                if (flag2 == true)
                {
                    strRecContent12 += "车辆颜色";
                    strRecContent22 += strColor;
                    strResult += strRecContent12;
                    strResult += "\n";
                    strResult += strRecContent22;
                    strResult += "\n\n";
                }
                //if (strShape != ""&&flag3==true)//处理用户自定义的配置结果
                if (flag3 == true)
                {
                    foreach (string s in strList)
                    {
                        if (s!="车辆数目"&&s!="车辆颜色")
                        {
                            strRecContent13 += s;
                        }
                    }
                    strRecContent23 += strShape;
                    strResult += strRecContent13;
                    strResult += "\n";
                    strResult += strRecContent23;
                    strResult += "\n\n";
                }

                //strRecContent3 += (dt2 - dt1).ToString();
                //strResult = strRecContent1 + "\n" + strRecContent2 + "\n" + strRecContent3;
                //strResult = strRecContent11 + "\n" + strRecContent21 + "\n" + strRecContent12 + "\n" + strRecContent22 + "\n" + strRecContent13 + "\n"+strRecContent23 + "\n";
                strResult += "用时：" + (dt2 - dt1).ToString();

                richTextBox_showResult.Text = strResult;
                //////////////////////////////////////////////////////////////////////////

                //////////////////////////////////////////////////////////////////////////
            }
            else
            {
                MessageBox.Show("请先配置算法和输出结果，再开始分析。");
            }
        }
        //保存图片
        //private void btn_savePic_Click(object sender, EventArgs e)
        //{
        //    //if (pictureBox_showResult.Image == pictureBox_showResult.InitialImage)
        //    //    MessageBox.Show("当前图片为空！");
        //    //else
        //    //{
        //    //    SaveFileDialog sfd = new SaveFileDialog();
        //    //    //sfd.Filter = "jpg图片(*.jpg)|*.jpg|bmp图片(*.bmp)|*.bmp";
        //    //    sfd.Filter = "bmp图片(*.bmp)|*.bmp";

        //    //    if (sfd.ShowDialog() == DialogResult.OK)
        //    //    {
        //    //        pictureBox_showResult.Image.Save(sfd.FileName);
        //    //    }
        //    //}
        //}

        //保存分析结果
        private void btn_saveResult_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "txt文档(*.txt)|*.txt";

            if (sfd.ShowDialog() == DialogResult.OK)
                richTextBox_showResult.SaveFile(sfd.FileName);
        }

        //清除分析结果
        private void btn_wipe_Click(object sender, EventArgs e)
        {
            richTextBox_showResult.Clear();
        }

        private void pictureMDI_Load(object sender, EventArgs e)
        {

        }
    }
}
